/*
 * inotify-sync - a simple file synchronizer and file system watcher
 * Copyright (C) 2010-2011, inotify-sync developers and inotify-sync contributors
 * Copyright (C) 2010-2011, Cohesion Network Security Studio
 *
 * inotify-sync is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * inotify-sync is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with inotify-sync; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
 * FILE:			Inotify.h
 * USAGE:			---
 * DESCRIPTION:		---
 * OPTIONS:			---
 * REQUIREMENTS:	---
 * BUGS:			---
 * NOTES:			---
 * AUTHOR:			LI Yiwei (mail:leeyiw@gmail.com)
 * COMPANY:			---
 * VERSION:			1.0
 * CREATED:			Thu Sep  1 13:42:03 CST 2011
 * REVISION:		---
 */

#ifndef INOTIFY
#define INOTIFY

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/inotify.h>
#include <ftw.h>
#include <string>
#include <vector>
#include <signal.h>
#include <setjmp.h>
#include <errno.h>
#include <pthread.h>

#include "EventHandler.h"
#include "Profile.h"

using namespace std;

struct DirNode
{
	int wd;
	string abs_path;
};

static vector<DirNode> dnode_vec;
static int fd;
static int do_init_dir(const char *fpath,
			const struct stat *sb,
			int typeflag,
			struct FTW *ftwbuf);
static void sig_alrm(int signo);
static jmp_buf env_alrm;
static void * read_events(void *arg);
static void * handle_events(void *arg);

class EventHandler;

class Inotify
{
public:
	Inotify(Profile &profile);
	virtual ~Inotify();
	int init_dir(const string root_dir);
	int destory_dir(const string root_dir);
	void monitor(time_t r_time);
	void get_event_handler(EventHandler *p_event_handler);
protected:
private:
	Profile profile;
	EventHandler *p_event_handler;

	//vector<DirNode> dnode_vec;
	vector<inotify_event *> ievent_p_vec;
};

#endif
